package com.ysx.article.listener;

import com.alibaba.fastjson.JSON;
import com.ysx.article.dto.ArticleVisitStreamMess;
import com.ysx.article.pojo.ApArticle;
import com.ysx.article.service.ApArticleService;
import com.ysx.common.constants.BusinessConstants;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 消费者消费消息
 *
 * @author YeShuXiang on 2021/10/10
 */
public class ArticleHostListener {

    @Component
    public class ArticleHotListener {

        @Autowired
        private ApArticleService apArticleService;

        @Autowired
        private StringRedisTemplate stringRedisTemplate;


        //接收streams流发送过来的消息
        @KafkaListener(topics = BusinessConstants.MqConstants.HOT_ARTICLE_INCR_HANDLE_TOPIC)
        public void receiveMessage(ConsumerRecord<String,String> record) {
            try {
                if(record!=null){
                    //1.获取到消息内容本身 JSON.toJSONString(articleVisitStreamMess)
                    String value = record.value();

                    //2.转成对象
                    ArticleVisitStreamMess articleVisitStreamMess = JSON.parseObject(value, ArticleVisitStreamMess.class);
                    //3.更新文章表的数据（点赞数，评论数,,,,,）
                    ApArticle apArticle = apArticleService.getById(articleVisitStreamMess.getArticleId());
                    if(apArticle!=null) {
                        //获取
                        Integer collect = articleVisitStreamMess.getCollect()==null?0:articleVisitStreamMess.getCollect().intValue();
                        Integer comment = articleVisitStreamMess.getComment()==null?0:articleVisitStreamMess.getComment().intValue();
                        Integer like = articleVisitStreamMess.getLike()==null?0:articleVisitStreamMess.getLike().intValue();
                        Integer view = articleVisitStreamMess.getView()==null?0:articleVisitStreamMess.getView().intValue();

                        if(apArticle.getLikes()!=null) {
                            apArticle.setLikes(apArticle.getLikes() + like);
                        }else{
                            apArticle.setLikes(like);
                        }

                        if(apArticle.getViews()!=null) {
                            apArticle.setViews(apArticle.getViews() + view);
                        }else{
                            apArticle.setViews(view);
                        }

                        if(apArticle.getComment()!=null) {
                            apArticle.setComment(apArticle.getComment() + comment);
                        }else{
                            apArticle.setComment(comment);
                        }

                        if(apArticle.getCollection()!=null) {
                            apArticle.setCollection(apArticle.getCollection() + collect);
                        }else{
                            apArticle.setCollection(collect);
                        }
                        apArticleService.updateById(apArticle);
                    }
                    //4.重新计算文章的分值
                    Integer score = apArticleService.computeScore(apArticle)*3;
                    // 呵呵开始排名所有的
                    stringRedisTemplate.boundZSetOps("HOT_ARTICLE_"+ BusinessConstants.ArticleConstants.DEFAULT_TAG).add(JSON.toJSONString(apArticle),Double.valueOf(score));
                    // 根据频道进行排名 key: 就是频道ID
                    stringRedisTemplate.boundZSetOps("HOT_ARTICLE_"+apArticle.getChannelId()).add(JSON.toJSONString(apArticle),Double.valueOf(score));

                }
            } catch (BeansException e) {
                e.printStackTrace();
            }
        }
    }





}
